package com.yyfax.ph.common.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import com.yyfax.common.tools.RobolectricUtils;
import com.yyfax.ph.R;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.home.getrecommendPermission.GetRecommendPermissionActivity;
import com.yyfax.ph.home.recommendloan.RecommendLoanActivity;
import com.yyfax.ph.personal.authority.bankcard.view.activity.BindCardActivity;
import com.yyfax.ph.personal.company.ShowBindCompanyActivity;
import com.yyfax.ph.personal.settings.GetCompanyModel;
import com.yyfax.ph.personal.subaccount.SubAccountActivity;

/**
 * Created by fan on 2017/12/5.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class RcmPermissionHelper {

	//账号（任意类型）被禁用
	public static void getRcmPermissionNoPermission(Context context) {
		ToastUtils.showToast(context, R.string.recommend_no_recommend_permission_tips);
	}

	//用户类型为母账户
	public static void getRcmPermissionIsParentAccount(final Context context) {
		if (RobolectricUtils.isARobolectricUnitTest()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setMessage(context.getString(R.string.recommend_use_sub_account_tips));
			builder.setNegativeButton(context.getString(R.string.cancel), null);
			builder.setPositiveButton(context.getString(R.string.browse_sub_account),
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialogInterface, int arg1) {
							dialogInterface.dismiss();
							SubAccountActivity.start((Activity) context);
						}
					}
			);
			AlertDialog alertDialog = builder.create();
			alertDialog.setCanceledOnTouchOutside(false);
			alertDialog.setCancelable(false);
			alertDialog.show();
		} else {
			showTipsDialog(context, context.getString(R.string.recommend_use_sub_account_tips), context.getString(R.string.cancel), context.getString(R.string.browse_sub_account)
					, new DialogBase.BottomTwoClickListener() {
						@Override
						public void onCancelClick(DialogBase dialog) {
							dialog.dismiss();
						}

						@Override
						public void onConfirmClick(DialogBase dialog) {
							dialog.dismiss();
							SubAccountActivity.start((Activity) context);
						}
					}, false);
		}
	}

	//注册时填写公司编号，被母账号拒绝
	public static void getRcmPermissionNoBindCompany(final Context context) {
		if (RobolectricUtils.isARobolectricUnitTest()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setMessage(context.getString(R.string.recommend_fill_in_company_number_tips));
			builder.setNegativeButton(context.getString(R.string.cancel), null);
			builder.setPositiveButton(context.getString(R.string.fill_in),
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialogInterface, int arg1) {
							dialogInterface.dismiss();
							Intent intent = new Intent(context, GetRecommendPermissionActivity.class);
							intent.putExtra(BindCardActivity.LAST_PAGE, BindCardActivity.FROM_HOME_FRAGMENT);
							GetRecommendPermissionActivity.start((Activity) context, intent);
						}
					}
			);
			AlertDialog alertDialog = builder.create();
			alertDialog.setCanceledOnTouchOutside(false);
			alertDialog.setCancelable(false);
			alertDialog.show();
		} else {
			showTipsDialog(context, context.getString(R.string.recommend_fill_in_company_number_tips),
					context.getString(R.string.cancel), context.getString(R.string.fill_in)
					, new DialogBase.BottomTwoClickListener() {
						@Override
						public void onCancelClick(DialogBase dialog) {
							dialog.dismiss();
						}

						@Override
						public void onConfirmClick(DialogBase dialog) {
							dialog.dismiss();
							Intent intent = new Intent(context, GetRecommendPermissionActivity.class);
							intent.putExtra(BindCardActivity.LAST_PAGE, BindCardActivity.FROM_HOME_FRAGMENT);
							GetRecommendPermissionActivity.start((Activity) context, intent);
						}
					}, false);
		}
	}

	//注册时填写公司编号，母账号未处理申请
	public static void getRcmPermissionApplying(Context context) {
		showTipsDialog(context, R.string.recommend_not_deal_bind_apply_tips, false);
	}

	//公司编号过期
	public static void getRcmPermissionChannelExpired(final Context context, final String company, final String phone) {
		if (RobolectricUtils.isARobolectricUnitTest()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setMessage(context.getString(R.string.recommend_unbind_company_tips));
			builder.setNegativeButton(context.getString(R.string.cancel), null);
			builder.setPositiveButton(context.getString(R.string.recommend_permission_unbind),
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialogInterface, int arg1) {
							dialogInterface.dismiss();
							Intent intent = new Intent(context, ShowBindCompanyActivity.class);
							intent.putExtra(ShowBindCompanyActivity.COMPANY, company);
							intent.putExtra(ShowBindCompanyActivity.STATUS, GetCompanyModel.STATUS_BIND);//绑定状态设为已绑定
							intent.putExtra(ShowBindCompanyActivity.PHONE, phone);
							intent.putExtra(ShowBindCompanyActivity.LAST_PAGE, ShowBindCompanyActivity.FROM_HOME_FRAGMENT);
							ShowBindCompanyActivity.start((Activity) context, intent);
						}
					}
			);
			AlertDialog mAlertDialog = builder.create();
			mAlertDialog.setCanceledOnTouchOutside(false);
			mAlertDialog.setCancelable(false);
			mAlertDialog.show();
		} else {
			showTipsDialog(context, context.getString(R.string.recommend_unbind_company_tips), context.getString(R.string.cancel)
					, context.getString(R.string.recommend_permission_unbind)
					, new DialogBase.BottomTwoClickListener() {
						@Override
						public void onCancelClick(DialogBase dialog) {
							dialog.dismiss();
						}

						@Override
						public void onConfirmClick(DialogBase dialog) {
							dialog.dismiss();
							Intent intent = new Intent(context, ShowBindCompanyActivity.class);
							intent.putExtra(ShowBindCompanyActivity.COMPANY, company);
							intent.putExtra(ShowBindCompanyActivity.STATUS, GetCompanyModel.STATUS_BIND);//绑定状态设为已绑定
							intent.putExtra(ShowBindCompanyActivity.PHONE, phone);
							intent.putExtra(ShowBindCompanyActivity.LAST_PAGE, ShowBindCompanyActivity.FROM_HOME_FRAGMENT);
							ShowBindCompanyActivity.start((Activity) context, intent);
						}
					}, false);
		}
	}

	//账户未经过实名认证
	public static void getRcmPermissionNoRealName(final Context context) {
		if (RobolectricUtils.isARobolectricUnitTest()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setMessage(context.getString(R.string.goto_bind_card_tips));
			builder.setNegativeButton(context.getString(R.string.cancel), null);
			builder.setPositiveButton(context.getString(R.string.recommend_permission_bind_card),
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialogInterface, int arg1) {
							dialogInterface.dismiss();
							Intent intent = new Intent(context, BindCardActivity.class);
							intent.putExtra(BindCardActivity.LAST_PAGE, BindCardActivity.FROM_HOME_FRAGMENT);
							BindCardActivity.start((Activity) context, intent);
						}
					}
			);
			AlertDialog alertDialog = builder.create();
			alertDialog.setCanceledOnTouchOutside(false);
			alertDialog.setCancelable(false);
			alertDialog.show();
		} else {
			showTipsDialog(context, context.getString(R.string.goto_bind_card_tips), context.getString(R.string.cancel),
					context.getString(R.string.recommend_permission_bind_card)
					, new DialogBase.BottomTwoClickListener() {
						@Override
						public void onCancelClick(DialogBase dialog) {
							dialog.dismiss();
						}

						@Override
						public void onConfirmClick(DialogBase dialog) {
							dialog.dismiss();
							Intent intent = new Intent(context, BindCardActivity.class);
							intent.putExtra(BindCardActivity.LAST_PAGE, BindCardActivity.FROM_HOME_FRAGMENT);
							BindCardActivity.start((Activity) context, intent);
						}
					}, false);
		}
	}

	public static void getRcmPermissionSuccess(Context context) {
		RecommendLoanActivity.start((Activity) context);
	}


	public static void getRecommendPassFailure(Context context, String tips) {
		ToastUtils.showToast(context, tips);
	}

	private static DialogBase mTipsDialog;

	public static void showTipsDialog(Context context, String tips, String tipsBottomOne, String tipsBottomTwo, DialogBase.BottomTwoClickListener listener, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, "", tips,
				tipsBottomOne, tipsBottomTwo, listener, cancelAble);
		mTipsDialog.show();
	}

	public static void showTipsDialog(Context context, int tips, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, context.getString(tips),
				context.getString(R.string.confirm),
				new DialogBase.BottomOneClickListener() {
					@Override
					public void bottomOneClick(DialogBase dialog) {
						dialog.dismiss();
					}
				}, cancelAble);
		mTipsDialog.show();
	}
}
